package com.taotao.trans;

import java.util.List;
import java.util.Map;
import java.util.Random;

import com.taotao.mapper.EyGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.taotao.common.pojo.BaseResult;
import com.taotao.controller.ApiSocketController;
import com.taotao.controller.SpringContext;
import com.taotao.mapper.EyAddressMapper;
import com.taotao.mapper.EyMemberMapper;
import com.taotao.mapper.EyRobotChatMapper;
import com.taotao.mapperCust.CommonMapperCust;
import com.taotao.pojo.EyGoods;
import com.taotao.pojo.EyMember;
import com.taotao.pojo.EyRobotChat;
import com.taotao.pojo.EyRobotChatExample;
import com.taotao.service.OperateLogService;
import com.taotao.service.WechatService;

@Service
public class RobotTrans {

	@Autowired
	EyAddressMapper eyAddressMapper;
	@Autowired
	WechatService wechatService;
	@Autowired
	EyMemberMapper eyMemberMapper;
	@Autowired
	EyGoodsMapper eyGoodsMapper;
	@Autowired
	OperateLogService operateLogService;
	@Autowired
	EyRobotChatMapper eyRobotChatMapper;
	@Autowired
	CommonMapperCust commonMapperCust;
	public static ApiSocketController apiSocketController;
	
	BaseResult baseResult = new BaseResult();
	
	    
    public static void init(){
    	apiSocketController = SpringContext.getBean(ApiSocketController.class);
    }

	public Map<String, Object> robotMsg(Map<String, String> map) {
		
		if(apiSocketController==null){
			RobotTrans.init();
        }
		String goodsId = map.get("goodsId");
		EyGoods goods = eyGoodsMapper.selectByPrimaryKey(Integer.parseInt(goodsId));
		EyMember member=eyMemberMapper.selectByPrimaryKey(goods.getMemberId());
		
		//获取开头聊天话术
		EyRobotChatExample exampleChat = new EyRobotChatExample();
		com.taotao.pojo.EyRobotChatExample.Criteria criteriaChat = exampleChat.createCriteria();
		criteriaChat.andTypeEqualTo("1");
		List<EyRobotChat> chatList = eyRobotChatMapper.selectByExample(exampleChat);
		
		String executeSql="select * from ey_member where 1=1"
				+ "	and source_id is not null "
				+ " and id not in "
				+ " (select sender from ey_session_info  where goods_id ='${goodsId}'  )"
				+ " and nick_name not like '%${nickName}%' "
				+ "order by rand() limit 1 ";
		
		executeSql=executeSql.replace("${goodsId}", goodsId);
		executeSql=executeSql.replace("${nickName}", member.getNickName());
		List<Map> executeMemberMap = commonMapperCust.executeSql(executeSql);
		Map memberMap = executeMemberMap.get(0);
		String sender = String.valueOf(memberMap.get("id"));
		
		
		JSONObject messageJson=new JSONObject();
		Random r=new Random();
		int index = r.nextInt(chatList.size()); //取随机数
		messageJson.put("content", chatList.get(index).getContent());
		messageJson.put("sender",sender);
		messageJson.put("receiver", member.getId());
		messageJson.put("modeType", "robot");
		messageJson.put("msgType", "1");
		messageJson.put("serviceName", "");
		messageJson.put("id", Integer.parseInt(goodsId));
		messageJson.put("sessionKey", "");
		apiSocketController.sendToUser(messageJson.toJSONString());
		
		return baseResult.getSuccMap();
	}
	

}
